﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JiuZuWuFu.Models;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace JiuZuWuFu.Controllers
{
    public class UserController : Controller
    {
        /// <summary>
        /// 从顶4级开始查询
        /// </summary>
        /// <param name="UserId">自己的主键</param>
        /// <param name="PedigreeId">家谱主键</param>
        /// <returns>返回6个对象型</returns>
        public IActionResult Index(int UserId = 1, int PedigreeId = 1)
        {
            JackGenealogyJiuZuWuFuContext zuWuFuContext = new JackGenealogyJiuZuWuFuContext();
            //获取所有人信息
            var users = (from userTb in zuWuFuContext.UserTb
                         join relationTb in zuWuFuContext.RelationTb.Where(x => x.PedigreeId == PedigreeId)
                         on userTb.UserId equals relationTb.UserId
                         where relationTb.PedigreeId == PedigreeId
                         select new UserList
                         {
                             UserId = userTb.UserId,
                             UserName = userTb.UserName,
                             ParentId = userTb.ParentId,
                             Sex = userTb.Sex
                         }).ToList();
            if (users != null && users.Any())
            {
                //首先找到自己4层的上级
                List<UserList> userList = new List<UserList>();
                //获取查看人的信息
                var entity = users.FirstOrDefault(x => x.UserId == UserId);
                entity.X = 0;
                entity.X = 0;
                if (entity != null)
                {
                    //添加自己
                    userList.Add(entity);
                    //删除提升效率
                    users.Remove(entity);
                }
                //首先找到自己的上级
                var ParentId = entity.ParentId;
                //获取上4级
                for (int i = 0; i < 4; i++)
                {
                    entity = users.FirstOrDefault(x => x.UserId == ParentId);
                    //没有上级直接跳出
                    if (entity == null)
                    {
                        break;
                    }
                    entity.X = 0;
                    entity.Y = i + 1;
                    //添加自己
                    userList.Add(entity);
                    ParentId = entity.ParentId;
                    //删除提升效率
                    users.Remove(entity);
                }
                //开始组装
                userList.ForEach(x =>
                {
                    assembleUserList(users, x);
                });
                var ss = JsonConvert.SerializeObject(userList);
                return Json(ss);
            }
            return Json("");
        }


        /// <summary>
        /// 从顶向下查找子级
        /// </summary>
        /// <param name="UserId">自己的主键</param>
        /// <param name="PedigreeId">家谱主键</param>
        /// <returns>返回一个对象</returns>
        public IActionResult Index2(int UserId = 1, int PedigreeId = 1)
        {
            JackGenealogyJiuZuWuFuContext zuWuFuContext = new JackGenealogyJiuZuWuFuContext();
            //获取所有人信息
            var users = (from userTb in zuWuFuContext.UserTb
                         join relationTb in zuWuFuContext.RelationTb.Where(x => x.PedigreeId == PedigreeId)
                         on userTb.UserId equals relationTb.UserId
                         where relationTb.PedigreeId == PedigreeId
                         select new UserList
                         {
                             UserId = userTb.UserId,
                             UserName = userTb.UserName,
                             ParentId = userTb.ParentId,
                             Sex = userTb.Sex
                         }).ToList();
            if (users != null && users.Any())
            {
                //最高层的人
                UserList userList = new UserList();
                //获取查看人的信息
                var entity = users.FirstOrDefault(x => x.UserId == UserId);
                //首先找到自己的上级
                var ParentId = entity.ParentId;
                //获取最上级
                for (int i = 0; i < 4; i++)
                {
                    entity = users.FirstOrDefault(x => x.UserId == ParentId);
                    //没有上级直接跳出
                    if (entity == null)
                    {
                        break;
                    }
                    //添加坐标
                    entity.X = 0;
                    entity.Y = i + 1;
                    //添加自己
                    userList = entity;
                    ParentId = entity.ParentId;
                }
                //递归查找
                assembleUserList(users, userList);
                var ss = JsonConvert.SerializeObject(userList);
                return Json(ss);
            }
            return Json("");

        }

        /// <summary>
        /// 从顶向下查找子级
        /// </summary>
        /// <param name="UserId">自己的主键</param>
        /// <param name="PedigreeId">家谱主键</param>
        /// <returns>返回一个对象</returns>
        public IActionResult 回朔法(int UserId = 1, int PedigreeId = 1)
        {
            JackGenealogyJiuZuWuFuContext zuWuFuContext = new JackGenealogyJiuZuWuFuContext();
            //获取所有人信息
            var users = (from userTb in zuWuFuContext.UserTb
                         join relationTb in zuWuFuContext.RelationTb.Where(x => x.PedigreeId == PedigreeId)
                         on userTb.UserId equals relationTb.UserId
                         where relationTb.PedigreeId == PedigreeId
                         select new UserList
                         {
                             UserId = userTb.UserId,
                             UserName = userTb.UserName,
                             ParentId = userTb.ParentId,
                             Sex = userTb.Sex
                         }).ToList();
            if (users != null && users.Any())
            {
                //最高层的人
                UserList userList = new UserList();
                //获取查看人的信息
                var entity = users.FirstOrDefault(x => x.UserId == UserId);
                //首先找到自己的上级
                var ParentId = entity.ParentId;
                //获取最上级
                for (int i = 0; i < 4; i++)
                {
                    entity = users.FirstOrDefault(x => x.UserId == ParentId);
                    //没有上级直接跳出
                    if (entity == null)
                    {
                        break;
                    }
                    //添加坐标
                    entity.X = 0;
                    entity.Y = i + 1;
                    //添加自己
                    userList = entity;
                    ParentId = entity.ParentId;
                }
                //递归查找
                assembleUserList(users, userList);
                var ss = JsonConvert.SerializeObject(userList);
                return Json(ss);
            }
            return Json("");

        }

        /// <summary>
        /// 拼接树
        /// </summary>
        /// <param name="userTbs">所有的人</param>
        /// <returns></returns>
        public List<UserList> 拼接树(List<UserList> userTbs)
        {
            //返回树
            List<UserList> list = new List<UserList>();
            //如果等于0就是顶级，直接添加到树的顶端,但是这个九族五服的最高只有一个
            Dictionary<string, UserList> pairs = new Dictionary<string, UserList>();
            userTbs.ForEach(x =>
            {
                pairs.Add(x.UserId.ToString(), x);
            });

            userTbs.ForEach(x =>
            {
                //判断有没有以他为父级的用户
                if (pairs.ContainsKey(x.ParentId.ToString()))
                {
                    if (x.Sex == 1)
                    {
                        pairs[x.ParentId.ToString()].left.Add(x);
                    }
                    else
                    {
                        pairs[x.ParentId.ToString()].right.Add(x);
                    }
                }

                //找到自己的父级
                var parent= userTbs.FirstOrDefault(x => x.UserId == x.ParentId);
                if (x.Sex == 1)
                {
                    parent.left.Add(x);
                }
                else
                {
                    parent.right.Add(x);
                }


            });













            return null;
        }
        /// <summary>
        /// 组装层级
        /// </summary>
        /// <param name="userTbs">家谱所有人</param>
        /// <param name="userList">找层级的自己</param>
        void assembleUserList(List<UserList> userTbs, UserList userList)
        {
            //4个父级找子级，子级是多个的
            var sublevels = userTbs.FindAll(x => x.ParentId == userList.UserId);
            sublevels.ForEach(x =>
            {
                //删除提高速度
                userTbs.Remove(x);
                x.Y = userList.Y - 1;
                //子级
                //区分男女 1是男
                if (x.Sex == 1)
                {
                    //求当前的坐标 适合男
                    x.X = (userList.X == 0 && userList.Y <= 0) ? userList.X : userList.X - 1;
                    if ((x.X > 0 ? x.X : x.X * -1) + (x.Y > 0 ? x.Y : x.Y * -1) > 4)
                    {
                        return;
                    }
                    userList.left.Add(x);
                    assembleUserList(userTbs, x);
                }
                else
                {
                    //求当前的坐标 适合女 自己子级男x的正整数就是女子的x注意看坐标
                    x.X = (userList.Y <= 0) ? (userList.X > 0 ? userList.X : userList.X * -1) : (userList.X - 1) * -1;
                    if ((x.X > 0 ? x.X : x.X * -1) + (x.Y > 0 ? x.Y : x.Y * -1) > 4)
                    {
                        return;
                    }
                    //只查询男性的九族五服，如果查询女性的就这里写递归
                    //assembleUserList(userTbs, x);
                    userList.right.Add(x);
                }
            });
        }

        void assembleUserList1(List<UserList> userTbs, UserList userList)
        {
            //4个父级找子级，子级是多个的
            var sublevels = userTbs.FindAll(x => x.ParentId == userList.UserId);
            while (true)
            {
                break;
            }
            sublevels.ForEach(x =>
            {
                //删除提高速度
                userTbs.Remove(x);
                x.Y = userList.Y - 1;
                //子级
                //区分男女 1是男
                if (x.Sex == 1)
                {
                    //求当前的坐标 适合男
                    x.X = (userList.X == 0 && userList.Y <= 0) ? userList.X : userList.X - 1;
                    if ((x.X > 0 ? x.X : x.X * -1) + (x.Y > 0 ? x.Y : x.Y * -1) > 4)
                    {
                        return;
                    }
                    userList.left.Add(x);
                    assembleUserList(userTbs, x);
                }
                else
                {
                    //求当前的坐标 适合女 自己子级男x的正整数就是女子的x注意看坐标
                    x.X = (userList.Y <= 0) ? (userList.X > 0 ? userList.X : userList.X * -1) : (userList.X - 1) * -1;
                    if ((x.X > 0 ? x.X : x.X * -1) + (x.Y > 0 ? x.Y : x.Y * -1) > 4)
                    {
                        return;
                    }
                    //只查询男性的九族五服，如果查询女性的就这里写递归
                    //assembleUserList(userTbs, x);
                    userList.right.Add(x);
                }
            });
        }

    }
}